import util from '@/libs/util';
import dateUtil from '../../util/getDate';
import regs from '../../util/formReg';

export default {
    data () {
        return {
            applyLoading:false,
            pageSize: 15,
            pageIndex: 1,
            total: 0,
            pageApplySize: 12,
            pageApplyIndex: 1,
            queryDataApply:{},
            totalApply: 0,
            entity:{},
            columns: [{
                width: 55,
                title: '#',
                type: 'selection'
            }, {
                title: '活动名称',
                key: 'name'
            }, {
                title: '活动地点',
                key: 'place'
            }, {
                title: '活动开始时间',
                key: 'startTime',
                render: function (h, params) {
                    let text = '';
                    if (regs.isNotEmpty(params.row.startTime)) {
                        text = dateUtil.getDate(params.row.startTime, 'date');
                    }
                    return h('span', {}, text);
                }
            }, {
                title: '活动结束时间',
                key: 'endTime',
                render: function (h, params) {
                    let text = '';
                    if (regs.isNotEmpty(params.row.startTime)) {
                        text = dateUtil.getDate(params.row.startTime, 'date');
                    }
                    return h('span', {}, text);
                }
            }, {
                title: '活动人数/已报名人数',
                key: 'k',
                render: function (h, params) {
                    let applyPeople = params.row.applyPeople;
                    let limitedPeople = params.row.limitedPeople;
                    return h('span', {}, limitedPeople + '人/' + applyPeople + '人');
                }
            }, {
                title: '活动费用(元)',
                key: 'amt'
            }, {
                title: '活动创建时间',
                key: 'createTime',
                render: function (h, params) {
                    let text = '';
                    if (regs.isNotEmpty(params.row.startTime)) {
                        text = dateUtil.getDate(params.row.startTime, 'date');
                    }
                    return h('span', {}, text);
                }
            }, {
                title: '活动状态',
                key: 'k',
                render: function (h, params) {
                    let status = params.row.status;
                    let text = '未知';
                    let style = {
                        'font-weight': 'bold'
                    };
                    switch (status) {
                        case 'APPLY':
                            text = '报名中';
                            style.color = 'green';
                            break;
                        case 'ACTIVITY':
                            text = '活动进行中';
                            style.color = 'blue';
                            break;
                        case 'END':
                            text = '活动结束';
                            style.color = 'red';
                            break;
                        case 'NOTRELEASE':
                            text = '未发布';
                            style.color = 'grey';
                            break;
                        default:
                            break;
                    }
                    return h('span', {
                        style: style
                    }, text);
                }
            }, {
                title: '操作',
                key: 'opt',
                render: (h, params) => {
                    if (params.row.status == 'APPLY') {
                        return h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.entity = params.row;
                                    this.$refs['formData'].resetFields();
                                    this.addVisible = true;
                                    this.findApplyInfo();
                                }
                            }
                        }, '查看已报名、报名');
                    }
                }
            }],
            dataList: [],
            queryData: {},
            addVisible: false,
            saveLoading: false,
            selection: [],
            applyColumns: [{
                width: 55,
                title: '#',
                type: 'selection'
            }, {
                title: '单号',
                key: 'p_code'
            },{
                title: '金额(元)',
                key: 'real_amt'
            },{
                title: '学生姓名',
                key: 'student_name'
            }, {
                title: '性别',
                key: 'sex',
                render: (h, params) => {
                    return h('div', {}, params.row.sex === 'MAN' ? '男' : (params.row.sex === 'WOMAN' ? '女' : '未知'));
                }
            }, {
                title: '出生日期',
                key: 'birthdate',
                render: function (h, params) {
                    return h('span', {}, dateUtil.getDate(params.row.birthdate, 'date'));
                }
            }, {
                title: '家长手机号',
                key: 'parentphone'
            }, {
                title: '状态',
                key: 'status',
                render: (h, params) => {
                    let status = params.row.status;
                    let text = '未知';
                    let style = {
                        'font-weight': 'bold'
                    };
                    switch (status) {
                        case 'NOPAY':
                            text = '待支付';
                            style.color = 'grey';
                            break;
                        case 'PAYED':
                            text = '已支付';
                            style.color = 'green';
                            break;
                        case 'REFUNDING':
                            text = '退费中';
                            style.color = 'red';
                            break;
                        case 'REFUNDED':
                            text = '已退费';
                            style.color = 'blue';
                            break;
                        default:
                            break;
                    }
                    return h('span', {
                        style: style
                    }, text);
                }
            }, {
                title: '操作',
                key: 'opt',
                render: (h, params) => {
                    let status = params.row.status;
                    if(status=='PAYED'){
                        return h('Button', {
                            props: {
                                type: 'success',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    util.ajax.post('/busine/activity/bill/getPrintShit', util.toFormData({// 查询订单详情
                                        id: params.row.id
                                    })).then(res => {
                                        this.$router.push({
                                            path: 'test',
                                            name: 'test',
                                            params: {
                                                data: res.data.data,
                                                entry: 1// 入口
                                            }
                                        });
                                    });
                                }
                            }
                        }, '打印发票');
                    }else if(status=='NOPAY'){
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                      this.chashPay(params.row);
                                    }
                                }
                            }, '现金缴费'),
                            h('Button', {
                                props: {
                                    type: 'success',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.weChatPayModal=true;
                                        this.orderDetails={};
                                        this.payNum='';
                                        this.settleWechatPay(params.row);
                                    }
                                }
                            }, '微信缴费'),
                            h('Button', {
                                props: {
                                    type: 'error',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.deleteApply(params.row);
                                    }
                                }
                            }, '删除')
                        ]);
                    }
                }
            }],
            applyDataList: [],
            formData: {},
            formDataRules: {
                name: [
                    { required: true, message: '请输入学生姓名', trigger: 'blur' }
                ]
            },
            gradeList:[],
            weChatPayModal:false,
            payNum:'',
            orderDetails:{},
            payLoading:false,
        };
    },
    mounted () {
        this.init();

        util.ajax.post('/base/datadictionary/findByCode', util.toFormData(
            {CODE: 'GRADE'}
        )).then(res => { // 获得区域数据
            if (res.data.code == 200) {
                var data = res.data.data;
                this.gradeList = data;
            }
        });
    },
    methods: {
        init: function () {
            this.doQuery();
        },
        doQuery: function () {
            let queryData = this.queryData;
            queryData.page = this.pageIndex;
            queryData.size = this.pageSize;
            queryData.status = ['NOTRELEASE', 'APPLY', 'ACTIVITY', 'END'];
            util.ajax.post('/busine/activity/list', util.toFormData(queryData)).then(res => {
                if (res.data.code == 200) {
                    this.dataList = res.data.data.list;
                    this.total = res.data.data.total;
                } else {
                    this.$Message.error(res.data.message);
                }
            });
        },
        showAdd: function () {
            // this.$refs['formData'].resetFields();
            this.addVisible = true;
        },
        doSave: function () {
            this.saveLoading = true;
            this.$refs['formData'].validate((valid) => {
                if (valid) {
                    util.ajax.post('/busine/activity/save', util.toFormData(this.formData)).then(res => {
                        this.saveLoading = false;
                        if (res.data.code == 200) {
                            this.$Message.success(res.data.message);
                            this.addVisible = false;
                        } else {
                            this.$Message.warning(res.data.message);
                        }
                    });
                } else {
                    this.$Message.error('请检查必填项');
                }
            });
        },
        doDel: function (ids) {
            if (ids) {
                this.$Modal.confirm({
                    title: '提示',
                    content: '确认删除？',
                    onOk: () => {
                        util.ajax.post('/busine/activity/delete', util.toFormData({ids: ids.join(',')})).then(res => {
                            if (res.data.code == 200) {
                                    this.$Message.success('删除成功！');
                                    this.pageIndex = 1;
                                    this.init();
                            }else {
                                this.$Message.warning(res.data.message || '未知错误！');
                            }
                        });
                    },
                    onCancel: () => {

                    }
                });
            } else {
                this.$Modal.error({content: '请选择删除项!'});
            }
        },
        whenSelectRow: function (selection) {
            this.selection = selection;
        },
        doDelBath: function (selection) { // 批量删除
            let list = this.selection;
            if (list.length == 0) {
                this.$Message.error('请选择删除项目');
                return;
            }
            var ids = [];
            for (let i in list) {
                ids.push(list[i].id);
            }
            this.doDel(ids);
        },
        doReleaseBath: function (selection) {
            let list = this.selection;
            if (list.length == 0) {
                this.$Message.warning('请选择发布项目');
                return;
            }
            var ids = [];
            for (let i in list) {
                ids.push(list[i].id);
            }
            this.doRelease(ids);
        },
        doRelease: function (ids) {
            if (ids) {
                this.$Modal.confirm({
                    title: '提示',
                    content: '确认发布？',
                    onOk: () => {
                        util.ajax.post('/busine/activity/doRelease', util.toFormData({ids: ids.join(',')})).then(res => {
                            if (res.data.code == 200) {
                                this.$Message.success(res.data.message);
                                this.pageIndex = 1;
                                this.init();
                            } else {
                                this.$Message.error(res.data.message || '未知错误！');
                            }
                        });
                    },
                    onCancel: () => {

                    }
                });
            } else {
                this.$Modal.warning({content: '请选择发布项!'});
            }
        },
        queryStudentByName: function (event) {
            let name = this.formData.name;
            if(regs.isNotEmpty(name)){
                util.ajax.post('/busine/student/detailsByName', util.toFormData({
                    name: name
                })).then(res => {
                    if (res.data.code == 200) {
                        let formData = res.data.data;
                        let birthdate=formData.birthdate;
                        if(regs.isNotEmpty(birthdate)){
                            formData.birthdate=new Date(birthdate);
                        }
                        this.formData=formData
                    }
                });
            }
        },
        doApply:function () {
            this.$refs['formData'].validate((valid)=>{
               if(valid){
                   this.applyLoading=true;
                   util.ajax.post('/busine/student/save', util.toFormData(this.formData)).then(res => {
                       this.applyLoading=false;
                       if (res.data.code == 200) {
                           let studentId = res.data.data.id;
                           let activityId=this.entity.id
                           util.ajax.post('/busine/activity/bill/doApply', util.toFormData({
                               studentId:studentId,
                               activityId:activityId
                           })).then(res => {
                               if (res.data.code == 200) {
                                   this.$Message.success(res.data.message);
                                   this.formData={};
                                    this.findApplyInfo();
                                    this.doQuery();
                               }else{
                                   this.$Message.error(res.data.message);
                               }
                           });
                       }else{
                           this.$Message.error(res.data.message);
                       }
                   });
               }else{
                   this.$Message.warning('请检查必填项');
               }
            });
        },
        findApplyInfo:function () {
            let queryData = this.queryDataApply
            queryData.activityId=this.entity.id;
            queryData.page=this.pageApplyIndex;
            queryData.size=this.pageApplySize;
            util.ajax.post('/busine/activity/bill/findApplyInfo', util.toFormData(queryData)).then(res => {
                if (res.data.code == 200) {
                    this.applyDataList=res.data.data.list;
                    this.totalApply=res.data.data.total;
                }else{
                    this.$Message.error(res.data.message);
                }
            });
        },
        reloadApplys:function () {
                 this.queryDataApply={};
                this.findApplyInfo();
        },
        payByWechat: function () {
            this.$Modal.confirm({
                title: '提示',
                content: '确认应收金额:' + this.orderDetails.real_amt + '(元)?',
                onOk: () => {
                    this.payLoading=true;
                    this.payNumVisible = true;
                    let payNum = this.payNum;
                    if (regs.isNotEmpty(payNum)) {
                        this.$Message.loading({
                            content: '支付中请稍后...',
                            duration: 0,
                            closable: true
                        });
                        util.ajax.post('/busine/activity/bill/wechatPay', util.toFormData({
                            id: this.orderDetails.id,
                            payNum: payNum
                        })).then(res => {
                            this.payLoading=false;
                            if (res.data.code == 1000) { // 免密支付成功
                                this.$Message.destroy();
                                this.$Notice.open({
                                    title: '支付消息',
                                    desc: '支付成功！',
                                    duration: 0
                                });
                                this.doQuery();
                                this.findApplyInfo();
                                // this.orderDetails.id = res.data.data.id;
                                // this.cleanPayShit();
                                //
                                // let data = res.data.data;
                                // data.student_name = this.orderDetails.name;
                                // data.real_amt = this.orderDetails.real_amt;
                                // let params = {
                                //     data: data,
                                //     entry: 2// 入口
                                // };
                                // this.goInvoicePage(params);
                                // this.queryWechatResult();
                            } else if (res.data.code == 1001 || res.data.code == 100) {
                                this.$Message.destroy();
                                this.$Notice.open({
                                    title: '支付消息',
                                    desc: res.data.message,
                                    duration: 0
                                });
                                // this.cleanPayShit();
                            } else if (res.data.code == 1002) {
                                this.queryWechatResult();
                            }
                        });
                    }else{
                        this.$Message.warning('支付条码为空');
                        this.payLoading=false;
                    }
                },
                onCancel: () => {

                }
            });
        },
        queryWechatResult: function () {
            util.ajax.post('/busine/activity/bill/wechatQuery', util.toFormData({
                id: this.orderDetails.id
            })).then(res => {
                this.payLoading=false;
                if (res.data.code == 1000) {
                    this.$Message.destroy();
                    this.$Notice.open({
                        title: '支付消息',
                        desc: '支付成功!',
                        duration: 0
                    });
                    this.weChatPayModal=false;
                    this.doQuery();
                    this.findApplyInfo();
                    // let data = res.data.data;
                    // data.student_name = this.orderDetails.name;
                    // data.real_amt = this.orderDetails.real_amt;
                    // let params = {
                    //     data: data,
                    //     entry: 2// 入口
                    // };
                    // this.goInvoicePage(params);
                } else if (res.data.code == 500 || res.data.code == 100) { // 业务逻辑错误
                    this.$Notice.open({
                        title: '支付消息',
                        desc: res.data.message,
                        duration: 0
                    });
                    // this.cleanPayShit();
                } else {
                    this.queryWechatResult();
                }
            });
        },
        settleWechatPay:function (data) {
            this.orderDetails.id=data.id;
            this.orderDetails.real_amt=data.real_amt;
        },
        deleteApply:function (data) {
            this.$Modal.confirm({
                title: '提示',
                content: '<p>确定删除？</p>',
                onOk: () => {
                    util.ajax.post('/busine/activity/bill/delete', util.toFormData({id: data.id,activityId:data.activity_id})).then(res => {
                        if (res.data.code == 200 ) {
                            this.$Message.success('操作成功！');
                            this.findApplyInfo();
                            this.doQuery();
                        } else {
                            this.$Message.error(res.data.message);
                        }
                    });
                },
                onCancel: () => {
                }
            });
        },
        chashPay:function (data) {
            this.$Modal.confirm({
                title: '提示',
                content: '确认支付:'+data.real_amt+"元?",
                onOk: () => {
                    util.ajax.post('/busine/activity/bill/cashPay', util.toFormData({id: data.id})).then(res => {
                        if (res.data.code == 200) {
                            this.$Message.success('操作成功！');
                            this.findApplyInfo();
                        } else {
                            this.$Message.error(res.data.message);
                        }
                    });
                },
                onCancel: () => {
                }
            });
        }
    }
};
